package com.qding.park.common.utils.rchl;



import java.security.Key;
import java.security.NoSuchAlgorithmException;

import javax.crypto.*;
import javax.crypto.spec.*;

public class RchlAESCoder {
	static final String algorithmStr = "AES/CBC/PKCS5Padding";
	static private KeyGenerator keyGen;
	static private Cipher cipher;
	static boolean isInited = false;

	private static byte[] IV = null;
	private static byte[] key1 = { 47, -82, 115, 67, 33, -85, -26, -61, -7,
			-114, 82, -18, 54, -3, -2, -128 };

	static private void init() {
		IV = "qpe1!#&%^&*;Dfog".getBytes();
		
		try {
			keyGen = KeyGenerator.getInstance("AES");
			cipher = Cipher.getInstance(algorithmStr);
			keyGen.init(128);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		}

		isInited = true;
	}

	public static byte[] GenKey() {
		if (!isInited) {
			init();
		}
		return keyGen.generateKey().getEncoded();
	}

	public static byte[] aesEncode(byte[] content, byte[] keyBytes) {
		if (keyBytes == null) {
			keyBytes = key1;
		}
		byte[] encryptedText = null;
		if (!isInited) {
			init();
		}
		Key key = new SecretKeySpec(keyBytes, "AES");

		try {
			IvParameterSpec iv = new IvParameterSpec(IV);
			cipher.init(Cipher.ENCRYPT_MODE, key, iv);
			encryptedText = cipher.doFinal(content);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return encryptedText;
	}
	
	public static String aesEncodeString(String content, byte[] keyBytes) {
		byte [] data = aesEncode(content.getBytes(),keyBytes);
		return parseByte2HexStr(data);
	}

	public static byte[] aesDecode(byte[] content, byte[] keyBytes) {
		if (keyBytes == null) {
			keyBytes = key1;
		}
		byte[] originBytes = null;
		if (!isInited) {
			init();
		}

		Key key = new SecretKeySpec(keyBytes, "AES");
		try {
			IvParameterSpec iv = new IvParameterSpec(IV);
			cipher.init(Cipher.DECRYPT_MODE, key, iv);
			originBytes = cipher.doFinal(content);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return originBytes;
	}
	
	
	public static String aesDecodeString(String content, byte[] keyBytes) {
		
		byte codebyte[] = parseHexStr2Byte(content);
		byte [] data = aesDecode(codebyte,keyBytes);

		return new String(data);
	}
	
	
	
	public static String parseByte2HexStr(byte buf[]) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
 
    }
	
	public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1)
            return null;
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),
                    16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }

}
